977E - Cyclic Components - CodeForces Solution


dfs and similar dsu graphs *1500

Please click on ads to support us..

Python Code:

import threading, sys


def main():
    n, m = map(int, input().split())
    graph = [[] for _ in range(n)]

    for _ in range(m):
        u, v = map(int, input().split())
        graph[u - 1].append(v)
        graph[v - 1].append(u)

    visited = [False for _ in range(n)]


    def check(node):
        nonlocal cycle
        visited[node - 1] = True
        if len(graph[node - 1]) != 2:
            cycle = False
        for neighbor in graph[node - 1]:
            if not visited[neighbor-1]:
                check(neighbor)
        ans = 0
    for n in range(n):
        if not visited[n]:
            node = n + 1
            cycle = True
            check(node)
            if cycle:
                ans += 1
    print(ans)

if __name__ == '__main__':
    sys.setrecursionlimit(1 << 30)
    threading.stack_size(1 << 27)

    main_thread = threading.Thread(target=main)
    main_thread.start()
    main_thread.join()

C++ Code:

#include <bits/stdc++.h>
#define int long long
#define endl '\n'
#define pii pair<int,int>
#define vint vector<int>
#define vpii vector<pii>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define unmap unordered_map
#define graph vector<vint>
#define clr(x) memset(x,0,sizeof(x));
using namespace std;
const int N = (int)2e5 + 69;
const int mod = (int)1e4 + 7;
int INF = INT_MAX;
int power(int a, int b) { int ans = 1; while (b) { if (b % 2) ans *= a; a *= a; b /= 2; } return ans; }
int gcd(int a, int b) { if (b == 0)return a;return gcd(b, a % b); }

graph g;
int vis[N];
int road[N];
bool tmp = 1;
void dfs(int node) {
    vis[node] = 1;
    if (g[node].size() != 2)
        tmp = 0;
    for (int c : g[node])
        if (!vis[c])
            dfs(c);
    return;
}
int messi = 10, cr7 = 7;
int32_t main() {

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, m;
    cin >> n >> m;
    g.resize(n + 1);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            dfs(i);
            ans += tmp;
            tmp = 1;
        }
    }
    cout << ans << endl;
}


Comments

Submit
0 Comments
More Questions

310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs